using UnityEngine;

public class Chicken : MonoBehaviour
{
	public Rigidbody2D rb;

	public bool isPet;

	public Animator animator;

	public StrollController sc;

	public AudioSource audioSource;

	public AudioClip layEggAudio;

	public AudioClip layShitAudio;

	public Body body;

	public AnimationCurve deployChance;

	public float minLayEggInterval;

	public float maxLayEggInterval;

	public int maxLayEgg;

	private int layEggCount;

	public GoldenEgg egg;

	public GameObject shit;

	public ParticleSystem diePs;

	public bool debug;

	private Vector2 defaultScale;

	private bool isDropEgg;

	public void Start()
	{
		if (!isPet)
		{
			CampaignGame.Instance.onWaveStart.AddListener(CheckAndDeploy);
			base.gameObject.SetActive(value: false);
		}
		sc.followTarget = Player.Instance.gameObject;
		defaultScale = base.transform.localScale;
		body.onDie.AddListener(Die);
		isDropEgg = false;
		Invoke("DropEggStart", Random.Range(minLayEggInterval, maxLayEggInterval));
	}

	public void CheckAndDeploy()
	{
		float num = deployChance.Evaluate(DO.Campaign.day) / (float)CampaignGame.Instance.waveTotal;
		if (Random.value < num && !base.gameObject.activeSelf)
		{
			sc.pause = false;
			base.gameObject.SetActive(value: true);
			base.transform.position = CampaignGame.Instance.player.transform.position + Vector3.right * 20f;
		}
	}

	private void FixedUpdate()
	{
		if (isDropEgg)
		{
			sc.pause = true;
			return;
		}
		sc.pause = false;
		Vector3 localScale = base.transform.localScale;
		if (rb.velocity.x > 0f)
		{
			localScale.x = 0f - defaultScale.x;
			base.transform.localScale = localScale;
		}
		else if (rb.velocity.x < 0f)
		{
			localScale.x = defaultScale.x;
			base.transform.localScale = localScale;
		}
		animator.SetFloat("speed", rb.velocity.magnitude);
	}

	private void Update()
	{
		if (body.health <= 0f)
		{
			body.Die();
		}
	}

	public void Die()
	{
		Object.Instantiate(diePs).transform.position = base.transform.position;
		if (!isPet)
		{
			CampaignGame.Instance.onWaveStart.RemoveListener(CheckAndDeploy);
		}
	}

	public void DropEggStart()
	{
		if (CampaignGame.Instance.isWaveCleared)
		{
			Invoke("DropEggStart", Random.Range(minLayEggInterval, maxLayEggInterval));
			return;
		}
		isDropEgg = true;
		animator.SetTrigger("dropEgg");
	}

	public void DropEggEnd()
	{
		isDropEgg = false;
		if (layEggCount < maxLayEgg)
		{
			audioSource.PlayOneShot(layEggAudio);
			Object.Instantiate(egg).transform.position = base.transform.position;
			layEggCount++;
		}
		else
		{
			audioSource.PlayOneShot(layShitAudio);
			Object.Instantiate(shit).transform.position = base.transform.position;
		}
		Invoke("DropEggStart", Random.Range(minLayEggInterval, maxLayEggInterval));
	}
}
